<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\ChatModel;
use common\service\ChatEditedAtUpdateService;


class UpdateChatEditedAtConsumer extends BaseConsumer
{

    // 要消费的队列名
    public $queue = 'update_chat_edited_at';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * @param $data
     * @throws \Exception
     */
    public function doConsume($data)
    {
        list($chatId, $editedAt, $xu) = ChatEditedAtUpdateService::getInstance()->pullUpdateEvent($data['chat_id']);
        // 空数据跳过
        if (empty($chatId) || empty($editedAt)) {
            return;
        }
        // 更新会话的编辑时间
        $chat = model(ChatModel::class)->modifyItem(['edited_at' => $editedAt, 'id' => $chatId]);
        // 工单会话 同步更新工单的排序时间
        if (!empty($chat) && $chat['link_type'] === 'task_work_order' && !empty($xu)) {
            try {
                teamones_request('teamones-saas', 'task_work_order/update', [
                    "data" => [
                        "id" => $chat['link_id'],
                        'im_edited_at' => $editedAt,
                    ],
                    'throw_no_data_change_exception' => "no"
                ]);
            } catch (\Throwable $e) {
                trace(format_exception_info($e), 'ERR');
            }
        }
    }

}
